# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=valgrind
pkgver=3.16.1
pkgrel=0
pkgdesc="A tool to help find memory-management problems in programs"
url="http://valgrind.org/"
arch="all"
license="GPL-2.0-or-later"
# it seems like busybox sed works but the configure script requires GNU sed
makedepends="sed paxmark perl bash autoconf automake libtool"
# from README_PACKAGERS:
#   Don't strip the debug info off lib/valgrind/$platform/vgpreload*.so
#   in the installation tree.  Either Valgrind won't work at all, or it
#   will still work if you do, but will generate less helpful error
#   messages.
options="!strip !check"
subpackages="$pkgname-dev $pkgname-doc"
source="ftp://sourceware.org/pub/$pkgname/$pkgname-$pkgver.tar.bz2
	musl.supp

	activate-musl-supp.patch
	uclibc.patch
	arm.patch"
#	musl-fixes.patch
builddir="$srcdir"/$pkgname-$pkgver

prepare() {
	default_prepare
	cd "$builddir"
	cp "$srcdir"/musl.supp .
	aclocal && autoconf && automake --add-missing
	echo '#include <linux/a.out.h>' > include/a.out.h
}

build() {
	cd "$builddir"
	# fails to build with ccache
	export CC="gcc"
	export CFLAGS="$CFLAGS -fno-stack-protector -no-pie"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--without-mpicc
	make
}

check() {
	cd "$buildir"
	make check
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" install

	# we have options=!strip above so we strip the /usr/bin/* manually
	if [ -z "$DEBUG" ]; then
		strip "$pkgdir"/usr/bin/valgrind \
			"$pkgdir"/usr/bin/valgrind-di-server \
			"$pkgdir"/usr/bin/vgdb \
			"$pkgdir"/usr/bin/valgrind-listener \
			"$pkgdir"/usr/bin/cg_merge
	fi

	# pax causes some issues
	# http://marc.info/?l=gentoo-hardened&m=119512627126298&w=2
	# http://bugs.alpinelinux.org/issues/999
	paxmark -m "$pkgdir"/usr/lib/valgrind/*-*-linux
}

sha512sums="2a4173efe1b6facdd2f5c5ee8ed006704168eba1813736fccc8191d60363afd96197512cf42037e65f18d4ddd49adc74a54c47210df216fba3c46bf68ef0f950  valgrind-3.16.1.tar.bz2
49df485f158a7f4d354b2ed0d46dcc691f0490f50913c83dabfdd513b47ef1045f140fd59f54b560df05e4f3a4aba63de7124553b396189fa3ac89c908831e45  musl.supp
09a992c39e755d92894f79cd95ed2a6e84daa3ac1af1fe8907ebb2c90dca27d224aae882389c2c246aff8a6bb7d8b9c57a6ca62bd7b36f6fb43e182471186821  activate-musl-supp.patch
d59a10db9037e120df2ee94a103402ca95a79abee9d8be63e4e1bca29c82dca775cc402a79b854ec11a2160a4d2da202c237369418e221d1925267ea2613fd5d  uclibc.patch
9ee297d1b2b86891584443ad0caadc4977e1447979611ccf1cc55dbee61911b0b063bc4ad936d86c451cedae410cb3219b5a088b2ad0aa17df182d564fe36cfe  arm.patch"
